/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.math.ColorRGBA;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.scene.Geometry;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.NiftyEventSubscriber;
import de.lessvoid.nifty.controls.ButtonClickedEvent;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import mygame.media.SimpleMediaPlayer;

/**
 * 测试音视频播放
 * @author JohnKkk 18402012144@163.com
 */
public class HelloMedia extends SimpleApplication implements ScreenController{
    private Nifty m_Nifty;
    private SimpleMediaPlayer m_MediaPlayer;

    @Override
    public void simpleInitApp() {
        setDisplayStatView(false);
        // 初始化Nifty
        NiftyJmeDisplay niftyDisplay = NiftyJmeDisplay.newNiftyJmeDisplay(
                assetManager,
                inputManager,
                audioRenderer,
                guiViewPort);
        m_Nifty = niftyDisplay.getNifty();
        // 将NiftyGUI显示对象添加到JME3中
        guiViewPort.addProcessor(niftyDisplay);
        
        m_Nifty.fromXml("Interface/IntroInfo.xml", "GScreen0", this);
        
        // 初始化mediaPlay
        initMediaPlayer();
        
        // 禁用flyCam并显示鼠标
        flyCam.setEnabled(false);
        inputManager.setCursorVisible(true);
    }
    
    @NiftyEventSubscriber(id = "Play")
    public final void playMedia(final String id, ButtonClickedEvent buttonClickedEvent){
        if(m_MediaPlayer.isLoaded()){
            if(!m_MediaPlayer.isPlaying()){
                m_MediaPlayer.playMedia();
            }
            else if(m_MediaPlayer.isPaused()){
                m_MediaPlayer.unpauseMedia();
            }
        }
    }
    
    @NiftyEventSubscriber(id = "Pause")
    public final void pauseMedia(final String id, ButtonClickedEvent buttonClickedEvent){
        if(m_MediaPlayer.isLoaded()){
            if(!m_MediaPlayer.isPaused()){
                m_MediaPlayer.pauseMedia();
            }
            else{
                m_MediaPlayer.unpauseMedia();
            }
        }
    }
    
    @NiftyEventSubscriber(id = "Reset")
    public final void resetMedia(final String id, ButtonClickedEvent buttonClickedEvent){
        if(m_MediaPlayer.isLoaded()){
            m_MediaPlayer.stopMedia();
            m_MediaPlayer.loadMedia();
        }
    }
    
    /**
     * 初始化mediaPlayer.<br/>
     */
    private void initMediaPlayer(){
        // Init player
        m_MediaPlayer = new SimpleMediaPlayer(this);
        // 原视频宽高比
        int movieWidth = 960;
        int movieHeight = 540;
        // 是否保持宽高比
        boolean keepAspect = true;
        float scaleRatio = 1.0f;
        int width = cam.getWidth();
        int height = cam.getHeight();
        if (keepAspect) {
            scaleRatio = ((float) width) / ((float) movieWidth);
            height = (int) (height * scaleRatio);
            movieWidth = width;
            movieHeight = height;
        }
        // Unique name
        String screenName = "Intro";
        // 播放器空闲时显示的图像。如果为空则使用屏幕颜色
        String idleImageAssetPath = "Media/idleImageAssetPath.jpg";
        // 播放器加载时显示的图像。如果为 Null，则使用 screenColor
        String loadingImageAssetPath = "Media/loadingImageAssetPath.jpg";
        // 播放器暂停时显示的图像。最后一帧为空
        String pausedImageAssetPath = "Media/pausedImageAssetPath.jpg";
        // 如果未提供以上图片则使用的颜色。
        ColorRGBA screenColor = ColorRGBA.Black;
        // 需要播放的视频
        String videoAssetPath = "Media/960_540.mjpg";
        // 需要播放的视频音轨（不需要则为null）
        String audioAssetPath = "Media/audio.ogg";
        // 源视频帧率。应与原始帧率一致。大多数情况下为 25 或 30
        int framesPerSec = 30;
        // 播放模式。播放一次或循环播放
        int playBackMode = SimpleMediaPlayer.PB_MODE_ONCE;
        // 屏幕透明度， 1用于intro, material and menu geometries. 小于1用于HUDgeometries
        float alpha = 1f;

        Geometry menuGeometry1 = m_MediaPlayer.genGeometry(screenName, movieWidth, movieHeight, idleImageAssetPath, loadingImageAssetPath, pausedImageAssetPath, screenColor, videoAssetPath, audioAssetPath, framesPerSec, playBackMode, alpha);
        // 保持宽高比的适合,需要调整位置
        if(keepAspect){
            menuGeometry1.setLocalTranslation(0, (cam.getHeight() - height) / 1.5f, 0);
        }
        // 加载视频和音轨资源
        m_MediaPlayer.loadMedia();
        // Add to gui 
        guiNode.attachChild(menuGeometry1);
    }

    @Override
    public void bind(Nifty nifty, Screen screen) {
    }

    @Override
    public void onStartScreen() {
    }

    @Override
    public void onEndScreen() {
    }

    @Override
    public void simpleUpdate(float tpf) {
        super.simpleUpdate(tpf); //To change body of generated methods, choose Tools | Templates.
        m_MediaPlayer.update(tpf);
    }
    
    public static void main(String[] args) {
        HelloMedia helloMedia = new HelloMedia();
        helloMedia.start();
    }
    
}
